from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader


class Calculator:

    def __init__(self):
        # 加载ui文件
        self.ui = QUiLoader().load('calculator.ui')

        # 执行点击事件
        self.action()


    # 绑定按钮事件
    def action(self):
        # 数字 0-9
        self.ui.pushButton_1.clicked.connect(self.one) # 1
        self.ui.pushButton_2.clicked.connect(self.two) # 2
        self.ui.pushButton_3.clicked.connect(self.three) # 3
        self.ui.pushButton_4.clicked.connect(self.four) # 4
        self.ui.pushButton_5.clicked.connect(self.five) # 5
        self.ui.pushButton_6.clicked.connect(self.six) # 6
        self.ui.pushButton_7.clicked.connect(self.seven) # 7
        self.ui.pushButton_8.clicked.connect(self.eight) # 8
        self.ui.pushButton_9.clicked.connect(self.nine) #9
        self.ui.pushButton_000.clicked.connect(self.zero) #0

        # 运算符号 + - * / =
        self.ui.pushButton_plus.clicked.connect(self.Plus) #+
        self.ui.pushButton_minusSign.clicked.connect(self.MinusSign) #-
        self.ui.pushButton_MultiplicationSign.clicked.connect(self.MultiplicationSign) #*
        self.ui.pushButton_divisionSign.clicked.connect(self.DivisionSign) #/
        self.ui.pushButton_decimalPoint.clicked.connect(self.decimalPoint)  # 小数点
        self.ui.pushButton_equal.clicked.connect(self.calculationResults)  # =

        # 清除 退格
        self.ui.pushButton_clear.clicked.connect(self.clear) #清除
        self.ui.pushButton_backspace.clicked.connect(self.backspace) #退格



    # 计算结果
    def calculationResults(self):
        # 添加等于号
        self.equal()
        # 获取文本框存在的内容
        get_lineEdit_old = self.ui.lineEdit_old.text().replace("=","")
        # 进行运算 eval()这个函数可以用于自动运算
        result = eval(get_lineEdit_old)
        print(f"运算结果：{result}")
        # 在文本框页面上显示
        self.ui.lineEdit_new.setText(str(result))

    # 按钮1
    def one(self):
        # 获取按钮上的文本
        one = self.ui.pushButton_1.text()
        # 显示到文本框上
        self.ui.lineEdit_old.insert(one)
        print(f"one:{one}")

    # 按钮2
    def two(self):
        # 获取按钮上的文本
        two = self.ui.pushButton_2.text()
        self.ui.lineEdit_old.insert(two)
        print(f"two:{two}")

    # 按钮3
    def three(self):
        # 获取按钮上的文本
        three = self.ui.pushButton_3.text()
        self.ui.lineEdit_old.insert(three)
        print(f"three:{three}")

    # 按钮4
    def four(self):
        # 获取按钮上的文本
        four = self.ui.pushButton_4.text()
        self.ui.lineEdit_old.insert(four)
        print(f"four:{four}")

    # 按钮5
    def five(self):
        # 获取按钮上的文本
        five = self.ui.pushButton_5.text()
        self.ui.lineEdit_old.insert(five)
        print(f"five:{five}")

    # 按钮6
    def six(self):
        # 获取按钮上的文本
        six = self.ui.pushButton_6.text()
        self.ui.lineEdit_old.insert(six)
        print(f"six:{six}")

    # 按钮7
    def seven(self):
        # 获取按钮上的文本
        seven = self.ui.pushButton_7.text()
        self.ui.lineEdit_old.insert(seven)
        print(f"seven:{seven}")

    # 按钮8
    def eight(self):
        # 获取按钮上的文本
        eight = self.ui.pushButton_8.text()
        self.ui.lineEdit_old.insert(eight)
        print(f"eight:{eight}")

    # 按钮9
    def nine(self):
        # 获取按钮上的文本
        nine = self.ui.pushButton_9.text()
        self.ui.lineEdit_old.insert(nine)
        print(f"nine:{nine}")

    # 按钮0
    def zero(self):
        # 获取按钮上的文本
        zero = self.ui.pushButton_000.text()
        self.ui.lineEdit_old.insert(zero)
        print(f"zero:{zero}")

    # 小数点
    def decimalPoint(self):
        # 获取按钮上的文本
        decimalPoint = self.ui.pushButton_decimalPoint.text()
        self.ui.lineEdit_old.insert(decimalPoint)
        print(f"小数点:{decimalPoint}")

    # 等于号
    def equal(self):
        # 获取按钮上的文本
        equal = self.ui.pushButton_equal.text()
        self.ui.lineEdit_old.insert(equal)
        print(f"等于号:{equal}")

    # 加
    def Plus(self):
        # 获取按钮上的文本
        plus = self.ui.pushButton_plus.text()
        self.ui.lineEdit_old.insert(plus)
        print(f"加号:{plus}")

    # 减
    def MinusSign(self):
        # 获取按钮上的文本
        minusSign = self.ui.pushButton_minusSign.text()
        self.ui.lineEdit_old.insert(minusSign)
        print(f"减号:{minusSign}")

    # 乘
    def MultiplicationSign(self):
        # 获取按钮上的文本
        multiplicationSign = self.ui.pushButton_MultiplicationSign.text()
        self.ui.lineEdit_old.insert(multiplicationSign)
        print(f"乘号:{multiplicationSign}")

    # 除
    def DivisionSign(self):
        # 获取按钮上的文本
        divisionSign = self.ui.pushButton_divisionSign.text()
        self.ui.lineEdit_old.insert(divisionSign)
        print(f"除号:{divisionSign}")


    # 清除
    def clear(self):
        # 清空文本框的内容
        self.ui.lineEdit_old.clear()

    # 退格
    def backspace(self):
        # 删除一个
        self.ui.lineEdit_old.backspace()


app = QApplication()
c = Calculator()
c.ui.show()
app.exec_()


